import axios from 'axios'
import Message from '@/components/message/message.vue'
const request = axios.create({
  baseURL: 'http://127.0.0.1:8000',
  timeout: 1000 * 100
})
// 响应拦截器
request.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  token && (config.headers.Authorization = token)
  return config
}, error => {
  return Promise.reject(error)
})
// 响应拦截器
request.interceptors.response.use(response => {
  let { data, meta } = response.data
  // 定义成功状态码集合
  const successStatus = [200, 201, 204];
  // 定义失败状态码集合
  const errorStatus = [400, 401, 403, 404, 422, 500];

  if (successStatus.includes(meta.status)) {
    Message.success(meta.msg);
  } else if (errorStatus.includes(meta.status)) {
    Message.error(meta.msg);
  } else {
    Message.success(meta.msg);
  }
  return data
}, error => {
  return Promise.reject(error)
})
export default request